<template>
  <!-- 整个购物车详情定位 -->
  <div class="main">
    <div class="main_cart">购物车</div>
    <div v-loading="loading">
      <el-card class="box-card" v-for="(item, index) in carList" :key="index">
        <div class="card_content">
          <div class="checkbox">
            <el-checkbox
              @change="handleCheck"
              v-model="selectList[index].checked"
            ></el-checkbox>
          </div>
          <img :src="item.cover_show" alt="" />

          <div class="content">
            <div class="title">{{ item.title }}</div>
            <div class="detail">
              <div>座位：{{ item.seat }}</div>
              <div>手机号码：{{ item.number }}</div>
              <div class="time">开场时间：</div>
              <div class="time_content" style="color: red; width: 200px">
                {{ item.times }}-{{ item.starTime }}
              </div>
            </div>
          </div>

          <div class="price" style="color: red; font-size: 18px">
            ￥{{ item.price }}
          </div>
          <el-button
            type="text"
            class="delete"
            style="color: red"
            @click="handleDelete(index)"
            >删除</el-button
          >
        </div>
      </el-card>

      <div class="card_finsh" v-if="carList.length !== 0">
        <div>
          <el-checkbox @change="handleCheckAll" v-model="checkAll"
            >全选</el-checkbox
          >

          <el-button type="text" class="settlement" @click="sumbitCart"
            >全部结算</el-button
          >
          <div class="total">
            <div class="text">合计：</div>
            <div class="total_price">￥{{ price }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 点击全部结束弹出框 -->
    <el-dialog
      title="扫码支付"
      :visible.sync="dialogVisible"
      width="21%"
      :show-close="false"
    >
      <div>
        <img src="../../assets/img/c.jpeg" alt="" class="c-img" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPay">支付成功</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectList: [],
      checkAll: false,
      loading: false,
      price: 0,
      carList: [
        {
          cover_show: require("../../assets/filmImg/xxyw.jpg"),
          title: "《小小愿望》",
          seat: "22号",
          number: "13567894563",
          times: "2021-12-09",
          starTime: "18:00",
          price: 30,
        },
        {
          cover_show: require("../../assets/filmImg/xxyw.jpg"),
          title: "《小小愿望》",
          seat: "22号",
          number: "13567894563",
          times: "2021-12-09",
          starTime: "18:00",
          price: 30,
        },
      ],
      dialogVisible: false,
    };
  },
  created() {
    this.loadCarts(); //钩子函数调用cart
  },
  mounted() {},
  methods: {
    //复选框change事件
    handleCheck() {
      this.checkOut();
    },

    //删除
    handleDelete(index) {
      this.carList.splice(index, 1);
      this.loadCarts();
    },
    //全选按钮
    handleCheckAll() {
      if (this.checkAll) {
        this.changeAllChecked(true);
        this.checkOut();
      } else {
        this.changeAllChecked(false);
        this.price = 0;
      }
    },
    //checked是否选中
    changeAllChecked(status) {
      for (var i = 0; i < this.selectList.length; i++) {
        this.selectList[i].checked = status;
      }
    },
    //全部结算按钮
    sumbitCart() {
      let result = this.selectList.filter((item) => {
        return item.checked;
      });
      if (result.length > 0) {
        this.$confirm(
          "请您仔细确认订单金额为" + this.price + "元，是否继续？",
          {
            confirmButtonText: "确认支付",
            cancelButtonText: "取消支付",
            type: "success",
            center: true,
          }
        )
          .then(() => {
            for (let i = 0; i < this.selectList.length; i++) {
              if (this.selectList[i].checked) {
                this.selectList[i].cart.status = 2;
                this.dialogVisible = true;
                this.i = i;
              }
            }
          })
          .catch(() => {
            for (let i = 0; i < this.selectList.length; i++) {
              if (this.selectList[i].checked) {
                this.selectList[i].cart.status = 0;
              }
            }
            this.$message({
              type: "warning",
              message: "用户已取消支付,请您前往我的订单进行支付",
            });
          });
      } else {
        this.$message({
          type: "warning",
          message: "请勾选要结算的电影",
        });
      }
    },
    //支付按钮
    submitPay() {
      this.dialogVisible = false;
    },
    //查询购物车后台接口
    loadCarts() {
      this.selectList = this.carList.map((item) => {
        //将目标文件数据复制到新的目录里
        return {
          checked: false,
          cart: Object.assign({}, item),
        };
      });
    },

    //封装购物车方法
    checkOut() {
      this.price = 0;
      for (var i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].checked) {
          this.price += this.selectList[i].cart.price;
        }
        if (!this.selectList[i].checked) {
          this.checkAll = false;
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.main {
  .main_cart {
    width: 100%;
    height: 90px;
    // background: pink;
    line-height: 90px;
    font-size: 20px;
    padding-left: 30px;
  }
  .box-card {
    width: 700px;
    height: 160px;
    // background: yellow;
    margin-left: 50px;
    margin-top: 25px;
    .card_content img {
      width: 100px;
      height: 130px;
      float: left;
    }
    .checkbox {
      float: left;
      margin-right: 15px;
      margin-top: 50px;
    }
    .content {
      float: left;
      .title {
        font-size: 20px;
        font-weight: bold;
        padding-left: 30px;
      }
      .detail {
        color: #aaa;
        font-size: 13px;
        padding-left: 40px;
        div {
          margin-top: 10px;
        }
        .time {
          float: left;
          // background: red;
          .time_content {
            float: left;
          }
        }
      }
    }
    .price {
      float: left;
      width: 50px;
      height: 50px;
      // background: red;
      margin-top: 40px;
      margin-left: 150px;
      line-height: 50px;
      // color: white;
    }
    .delete {
      width: 50px;
      height: 50px;
      font-size: 18px;
      // line-height: 50px;
      margin-right: 20px;
      margin-top: 40px;

      // background: pink;

      float: right;
    }
  }
  .card_finsh {
    width: 700px;
    height: 50px;
    background: gray;
    margin-left: 50px;
    margin-top: 10px;
    .el-checkbox {
      float: left;
      // background: gray;
      width: 60px;
      height: 20px;
      margin: 15px;
      margin-left: 25px;
    }
    .total {
      width: 110px;
      height: 50px;
      // background: red;
      // margin-right: 10px;
      float: right;
      .text {
        float: left;
        line-height: 50px;
      }
      .total_price {
        float: left;
        line-height: 50px;
        // background: red;
      }
    }
    .settlement {
      width: 140px;
      height: 50px;
      float: right;
      background: red;
      color: black;
      font-size: 20px;
      padding-left: 20px;
      padding-right: 20px;
      // margin-right: 20px;
    }
  }
  .el-dialog img {
    width: 285px;
    height: 285px;
  }
}
</style>